package pack.be;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class ListIndicats extends ListActivity{
	
	private ListView listIndicat;
	private Button disConnectButton;
	
	 /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.projectsview);
        
        try {
			remplirListeIndic();
		} catch (Exception e) {
			e.printStackTrace();
		}
        
        disConnectButton = (Button)findViewById(R.id.disConnectBut);
        
        disConnectButton.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View arg0) {
				Intent intent = new Intent(ListIndicats.this, BEAndroidActivity.class);
				startActivity(intent);
			}
		});
        
        Intent intent = getIntent();
        setListAdapter(new ArrayAdapter<String>(this, R.layout.indicatlistview, listeIndicat));
        
        listIndicat = getListView();
        listIndicat.setTextFilterEnabled(true);
        
      //si on clique sur un indicateur on doit pouvoir acceder au graphique correspondant
        listIndicat.setOnItemClickListener(new OnItemClickListener() {
  	    public void onItemClick(AdapterView<?> parent, View view,
  	        int position, long id) {
  	      // acceder a la liste d'indicateur 
  	    	
  	    	Intent intent = new Intent(ListIndicats.this, PlotActivity.class);
  	    	intent.putExtra("indicator", listeIndicat[position]);
  	    	startActivity(intent);
  	    	
  	      }
    	});
        
    }
    
  //recuperer liste des indicateurs ds la bdd
    public String[] listeIndicat; // = new String[]{"indicat1", "indicat2", "indicat3", "indicat4"};

    public void remplirListeIndic() throws Exception{
    	ConnectionPostgre con = new ConnectionPostgre();
    	con.connection();
    	String resultat[];
    	int cont = 0, i = 0;
    	
    	resultat = con.indicateurProjet(1);
    	listeIndicat = new String[(resultat.length)];
    	while(cont < listeIndicat.length){
    		listeIndicat[i] = resultat[cont];
    		cont++;
    		i++;
    	}
    	
    	con.deconnection();
    }
}
